using System;
using System.Collections.Generic;
using System.Web.UI;
using mojoPortal.Web.Framework;
using mojoPortal.Web;
using Resources;
using System.Text;
using ud_Video.Business;
using System.Configuration;
using System.Web.UI.WebControls;

namespace ud_Video.UI
{
    public partial class VideoModule : SiteModuleControl
    {
        protected VideoConfiguration config = new VideoConfiguration();
        readonly DBVideoClip repository = new DBVideoClip();
        ud_VideoClip item = new ud_VideoClip();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
            EnableViewState = false;
        }

        
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            LoadSettings();
            PopulateLabels();
            PopulateControls();
            SetUpJQuery();
        }

        private void SetUpJQuery()
        {
            if (item == null) return;
            string path = ImageSiteRoot + "/" + ConfigurationManager.AppSettings["VideoFolder"];
            StringBuilder sb = new StringBuilder();
            sb.Append("<script src='" + ImageSiteRoot +
                      "/ClientScript/media/jwplayer.js' type='text/javascript'></script>");
            sb.Append("<script type='text/javascript'>");
            sb.Append("jwplayer('" + pnlVideo.ClientID + "').setup({");
            sb.Append("'flashplayer': '" + ImageSiteRoot + "/mediaplayer.swf',");
            sb.Append("'id': 'playerID',");
            if(config.AutoStart)
            {
                sb.Append("'autostart': 'true',");
            }
            sb.Append("'width': '" + config.Width + "',");
            sb.Append("'height': '" + config.Height + "',");
            sb.Append("'file': '" + path + item.VideoURL + "'");
            if (item.ImageURL != string.Empty)
            {
                sb.Append(",'image': '" + path + item.ImageURL + "'");
            }
            sb.Append("});");

            sb.Append("$(document).ready(function(){");
            if (config.UseDropDownList)
            {
                sb.Append("$('.listvideo" + ModuleId.ToInvariantString() + "').change(function(){");
                sb.Append("jwplayer('" + pnlVideo.ClientID + "').setup({");
                sb.Append("'flashplayer': '" + ImageSiteRoot + "/mediaplayer.swf',");
                sb.Append("'id': 'playerID',");
                sb.Append("'width': '" + config.Width + "',");
                sb.Append("'height': '" + config.Height + "',");
                if (config.ClickAutoStart)
                {
                    sb.Append("'autostart': 'true',");
                }
                sb.Append("'file': '" + path + "' +$(this).val() + ''");
                sb.Append("});");
                sb.Append("});");
            }
            if (config.UseList)
            {
                sb.Append("$('.video" + ModuleId.ToInvariantString() + "').click(function(){");
                sb.Append("jwplayer('" + pnlVideo.ClientID + "').setup({");
                sb.Append("'flashplayer': '" + ImageSiteRoot + "/mediaplayer.swf',");
                sb.Append("'id': 'playerID',");
                sb.Append("'width': '" + config.Width + "',");
                sb.Append("'height': '" + config.Height + "',");
                if (config.ClickAutoStart)
                {
                    sb.Append("'autostart': 'true',");
                }
                sb.Append("'file': '" + path + "' + $(this).attr('relvideo') + '',");
                sb.Append("'image': '" + path + "' + $(this).attr('relimage') + ''");
                sb.Append("});");

                sb.Append("$('.module" + ModuleId.ToInvariantString() + " .videoitem').removeClass('selected');");
                sb.Append("$(this).addClass('selected');");

                sb.Append("});");
            }
            sb.Append("});");
            sb.Append("</script>");

            if (!Page.ClientScript.IsStartupScriptRegistered(typeof (Page), "video" + ModuleId.ToInvariantString()))
            {
                Page.ClientScript.RegisterStartupScript(typeof (Page), "video" + ModuleId.ToInvariantString(),
                                                        sb.ToString());
            }
        }

        private void PopulateControls()
        {
            BindListVideo();
        }

        private void BindListVideo()
        {
            item = repository.GetLastest(ModuleId);
            if (item == null) return;
            if (config.UseDropDownList)
            {
                DropDownList ddlListVideo = new DropDownList
                                                {
                                                    DataSource = repository.GetAll(ModuleId),
                                                    DataTextField = "Name",
                                                    DataValueField = "VideoURL",
                                                    CssClass = "listvideo" + ModuleId.ToInvariantString()
                                                };
                ddlListVideo.DataBind();
                placeHolder.Controls.Add(ddlListVideo);
            }
            if (!config.UseList) return;
            List<ud_VideoClip> listVideo = repository.GetVideos(ModuleId, config.NumberVideo);
            bool firstItem = true;
            foreach (ud_VideoClip videoClip in listVideo)
            {
                Label lblVideoItem;
                if (firstItem)
                {
                    lblVideoItem = new Label {CssClass = "videoitem selected video" + ModuleId, Text = videoClip.Name};
                    firstItem = false;
                }
                else
                {
                    lblVideoItem = new Label { CssClass = "videoitem video" + ModuleId, Text = videoClip.Name };
                }
                lblVideoItem.Attributes.Add("relvideo", videoClip.VideoURL);
                lblVideoItem.Attributes.Add("relimage", videoClip.ImageURL);
                placeHolder.Controls.Add(lblVideoItem);
            }
        }

        protected virtual void PopulateLabels()
        {
            Title1.EditUrl = SiteRoot + "/VideoClip/Admin/VideoEdit.aspx";
            Title1.EditText = VideoResource.EditPage;
            pnlContent.CssClass += " content" + ModuleId.ToInvariantString();
        }

        protected virtual void LoadSettings()
        {
            pnlContainer.ModuleId = ModuleId;
            config = new VideoConfiguration(Settings);
            if (!config.InstanceCssClass.Equals(string.Empty)) { pnlContainer.CssClass += " " + config.InstanceCssClass; }
            if (ModuleConfiguration == null) return;
            Title = ModuleConfiguration.ModuleTitle;
            Description = ModuleConfiguration.FeatureName;
        }
    }
}
